package com.incar.chuxing.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.incar.chuxing.R;
import com.incar.chuxing.utils.DensityUtils;
import com.incar.chuxing.utils.Dialogs.ChooseHoursDialog;
import com.incar.chuxing.utils.SPKey;
import com.incar.chuxing.utils.SPUtils;
import com.incar.chuxing.utils.TimeUtils;

import java.util.Date;

public class TabSwitchView extends RelativeLayout {
    private static final int HORIZONTAL = 0;
    private static final int VERTICAL = 1;
    private int selected;
    private String littleDaySelectedId;
    private View view;
    private String endTime;
    private int orientationStyle;
    private OnTabChangeListener onTabChangeListener;

    public TabSwitchView(Context context) {
        super(context);
        initView(context);
    }


    public TabSwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TabSwitchView);
        orientationStyle = ta.getInt(R.styleable.TabSwitchView_orientationStyle, HORIZONTAL);
        initView(context);
    }

    private TextView tabShare;
    private TextView tabDay;
    private TextView tabMinDay;
    private RelativeLayout tabLayout;
    private RelativeLayout layoutChoose;//主页面选择时间 和 小日租选择时间后
    private RelativeLayout layout_dayRent;//日租选择时间后
    private ImageView chooseReturn;//返回按钮
    private RelativeLayout tabLayoutInner;
    private TextView chooseTime;//请选择用车时长
    private TextView day_choose_duration;
    private TextView day_choose_time;
    private RelativeLayout layout_choose_day;//扫车牌的日租选择时间 和 选择后

    private void initView(Context context) {
        if (orientationStyle == HORIZONTAL) {
            LayoutInflater.from(context).inflate(R.layout.layout_tab_switch_horizontal, this);
        } else {
            LayoutInflater.from(context).inflate(R.layout.layout_tab_switch_vertical, this);
        }
        tabLayout = findViewById(R.id.tab_layout);
        layoutChoose = findViewById(R.id.layout_choose);
        view = findViewById(R.id.tab_bg);
        tabShare = findViewById(R.id.tab_share);
        tabDay = findViewById(R.id.tab_day);
        tabMinDay = findViewById(R.id.tab_minDay);
        chooseReturn = findViewById(R.id.choose_return);
        tabLayoutInner = findViewById(R.id.tab_layout_inner);
        chooseTime = findViewById(R.id.choose_time);
        day_choose_time = findViewById(R.id.day_choose_time);
        day_choose_duration = findViewById(R.id.day_choose_duration);
        layout_dayRent = findViewById(R.id.layout_dayRent);
        SpannableString spannableString = new SpannableString("小日租\n(4小时起)");
        spannableString.setSpan(new AbsoluteSizeSpan(DensityUtils.dp2px(getContext(), 9)), 4, 10, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tabMinDay.setText(spannableString);
        tabShare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selected != 0) {
                    endTime = "0";
                    littleDaySelectedId = "0";

                    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", view.getX(), 0).setDuration(200);
                    animator.setInterpolator(new AccelerateInterpolator());
                    animator.addUpdateListener(animation -> {
                        if (orientationStyle == HORIZONTAL) {
                            if (animation.getAnimatedFraction() > 0.6) {
                                tabShare.setTextColor(Color.parseColor("#ffffff"));
                                tabDay.setTextColor(Color.parseColor("#323232"));
                                tabMinDay.setTextColor(Color.parseColor("#323232"));
                            }
                        } else {
                            layoutChoose.setVisibility(GONE);
                            if (layout_choose_day != null)
                                layout_choose_day.setVisibility(GONE);
                            layout_dayRent.setVisibility(GONE);

                        }
                    });
                    animator.start();
                    selected = 0;
                    if (onTabChangeListener != null) {
                        onTabChangeListener.onTabChange(selected);
                    }
                }
            }
        });


        tabMinDay.setOnClickListener(v -> {
            if (selected != 1) {
                endTime = "0";
                littleDaySelectedId = "0";
                ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", view.getX(), tabLayoutInner.getWidth() / 3).setDuration(200);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        if (animation.getAnimatedFraction() > 0.6) {
                            tabShare.setTextColor(Color.parseColor("#323232"));
                            tabDay.setTextColor(Color.parseColor("#323232"));
                            tabMinDay.setTextColor(Color.parseColor("#ffffff"));
                        }
                        if (animation.getAnimatedFraction() == 1) {
                            if (orientationStyle == HORIZONTAL) {
                                layout_dayRent.setVisibility(GONE);
                                chooseTime.setVisibility(VISIBLE);
                                chooseTime.setText("请选择用车时长");
                                layoutChoose.setVisibility(VISIBLE);
                                ObjectAnimator animator = ObjectAnimator.ofFloat(layoutChoose, "translationX", getWidth(), 0).setDuration(200);
                                animator.setInterpolator(new AccelerateInterpolator());
                                animator.start();
                            } else {
                                if (layout_choose_day != null)
                                    layout_choose_day.setVisibility(GONE);
                                layoutChoose.setVisibility(VISIBLE);
                                chooseTime.setVisibility(VISIBLE);
                                chooseTime.setTextColor(Color.parseColor("#aaaaaa"));
                                chooseTime.setText("请选择用车时长");
                            }
                        }
                    }
                });

                animator.start();
                selected = 1;
                if (onTabChangeListener != null) {
                    onTabChangeListener.onTabChange(selected);
                }
            }
        });

        tabDay.setOnClickListener(v -> {
            if (selected != 2) {
                endTime = "0";
                littleDaySelectedId = "0";
                ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", view.getX(), tabLayoutInner.getWidth() / 3 * 2).setDuration(200);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        if (animation.getAnimatedFraction() > 0.6) {
                            tabShare.setTextColor(Color.parseColor("#323232"));
                            tabDay.setTextColor(Color.parseColor("#ffffff"));
                            tabMinDay.setTextColor(Color.parseColor("#323232"));
                        }
                        if (animation.getAnimatedFraction() == 1) {
                            if (orientationStyle == HORIZONTAL) {
                                chooseTime.setVisibility(VISIBLE);
                                layout_dayRent.setVisibility(GONE);
                                chooseTime.setText("请选择还车时间");
                                layoutChoose.setVisibility(VISIBLE);
                                ObjectAnimator animator = ObjectAnimator.ofFloat(layoutChoose, "translationX", getWidth(), 0).setDuration(200);
                                animator.setInterpolator(new AccelerateInterpolator());
                                animator.start();
                            } else {
                                if (layout_choose_day == null) {
                                    layout_choose_day = findViewById(R.id.layout_choose_day);

                                }
                                layout_choose_day.setVisibility(VISIBLE);
                                layoutChoose.setVisibility(GONE);
                                layout_dayRent.setVisibility(GONE);
                                //扫码日租选时间
                                layout_choose_day.setOnClickListener(vi -> {
                                    long maxTime = System.currentTimeMillis() + (SPUtils.getInstance(getContext()).getIntValueWithDef(SPKey.DAY_RENT_DURATION, 14400) * 60 * 1000);
                                    MyTimePicker timePicker = new MyTimePicker(context, 20, new Date().getTime(), maxTime);
                                    timePicker.setOnTimeChosenListener(new MyTimePicker.OnTimeChosenListener() {
                                        @Override
                                        public void onTimeChosen(long timeMills) {
                                            //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                                            // longRentRentTime.setText(simpleDateFormat.format(new Date(timeMills)));
                                            onTabChangeListener.onTimeChosen(timeMills + "");
                                        }
                                    });
                                    timePicker.showPicker();
                                });

                            }
                        }
                    }
                });
                animator.start();

                selected = 2;
                if (onTabChangeListener != null) {
                    onTabChangeListener.onTabChange(selected);
                }
            }
        });

        chooseReturn.setOnClickListener(v -> {
            if (orientationStyle == HORIZONTAL) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(layoutChoose, "translationX", 0, getWidth()).setDuration(200);
                animator.setInterpolator(new AccelerateInterpolator());
                animator.start();
                tabShare.callOnClick();
            }

        });

        chooseTime.setOnClickListener(v -> {
            if (selected == 1) {
                ChooseHoursDialog chooseHoursDialog = new ChooseHoursDialog(getContext(), 0);
                chooseHoursDialog.setYesOnclickListener((selectedId, selectedText) -> {
                    chooseTime.setText("用车时长：" + selectedText);
                    chooseTime.setTextColor(getResources().getColor(R.color.black));
                    onTabChangeListener.onHoursChosen(selectedId);
                    littleDaySelectedId = selectedId;
                    chooseHoursDialog.dismiss();
                });
                chooseHoursDialog.setNoOnclickListener(() -> chooseHoursDialog.dismiss());
                chooseHoursDialog.show();

            } else if (selected == 2) {
                long maxTime = System.currentTimeMillis() + (SPUtils.getInstance(getContext()).getIntValueWithDef(SPKey.DAY_RENT_DURATION, 14400) * 60 * 1000);
                MyTimePicker timePicker = new MyTimePicker(context, 20, new Date().getTime(), maxTime);
                timePicker.setOnTimeChosenListener(new MyTimePicker.OnTimeChosenListener() {
                    @Override
                    public void onTimeChosen(long timeMills) {
                        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                        // longRentRentTime.setText(simpleDateFormat.format(new Date(timeMills)));
                        onTabChangeListener.onTimeChosen(timeMills + "");
                        endTime = timeMills + "";
                        chooseTime.setVisibility(GONE);
                        layout_dayRent.setVisibility(VISIBLE);
                        day_choose_time.setText(TimeUtils.formatDay(Long.parseLong(timeMills + "")));

                    }
                });
                timePicker.showPicker();
            } else {
                layout_dayRent.setVisibility(VISIBLE);
            }
        });

        layout_dayRent.setOnClickListener(v -> {
            long maxTime = System.currentTimeMillis() + (SPUtils.getInstance(getContext()).getIntValueWithDef(SPKey.DAY_RENT_DURATION, 14400) * 60 * 1000);
            MyTimePicker timePicker = new MyTimePicker(context, 20, new Date().getTime(), maxTime);
            timePicker.setOnTimeChosenListener(new MyTimePicker.OnTimeChosenListener() {
                @Override
                public void onTimeChosen(long timeMills) {
                    //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                    // longRentRentTime.setText(simpleDateFormat.format(new Date(timeMills)));
                    onTabChangeListener.onTimeChosen(timeMills + "");
                    endTime = timeMills + "";
                    chooseTime.setVisibility(GONE);
                    layout_dayRent.setVisibility(VISIBLE);
                    day_choose_time.setText(TimeUtils.formatDay(Long.parseLong(timeMills + "")));

                }
            });
            timePicker.showPicker();
        });
    }


    public void setOnTabChangeListener(OnTabChangeListener onTabChangeListener) {
        this.onTabChangeListener = onTabChangeListener;
    }

    //选择的index
    public int getSelected() {
        return selected;
    }


    public String getLittleDaySelectedId() {
        if (littleDaySelectedId == null) {
            return null;
        } else if (littleDaySelectedId.equals("0")) {
            return null;
        }
        return littleDaySelectedId;
    }


    public void setDefault() {
        view.setX(0);
        view.setY(0);
        tabShare.setTextColor(Color.parseColor("#ffffff"));
        tabDay.setTextColor(Color.parseColor("#323232"));
        tabMinDay.setTextColor(Color.parseColor("#323232"));
        selected = 0;
        endTime = "0";
        littleDaySelectedId = "0";
        if (layout_choose_day != null)
            layout_choose_day.setVisibility(GONE);
        layoutChoose.setVisibility(GONE);
        layout_dayRent.setVisibility(GONE);
    }

    public void setDayRentDuration(String timeMills, String preRentTime) {
        if (selected == 2) {
            endTime = timeMills;
            layout_dayRent.setVisibility(VISIBLE);
            day_choose_time.setText(TimeUtils.formatDay(Long.parseLong(timeMills)));
            day_choose_duration.setText("租期\n" + preRentTime);
        }
    }


    public String getEndTime() {
        if (endTime == null) {
            return null;
        } else if (endTime.equals("0")) {
            return null;
        }
        return endTime;
    }

    public interface OnTabChangeListener {
        void onTabChange(int selectedIndex);

        void onHoursChosen(String selectedId);//小时租用车小时数

        void onTimeChosen(String timeMills);//日租还车时间

    }


    public boolean isChosenTime() {
        if (selected == 0) {
            return true;
        } else if (selected == 1 && !littleDaySelectedId.equals("0")) {
            return true;
        } else if (selected == 2 && !endTime.equals("0")) {
            return true;
        } else {
            return false;
        }
    }
}
